﻿namespace SharpF2.ModelView
{
	using System;
	using System.Windows.Input;
	using Commands;
	using Properties;
	using Services;

	public class UserCommentDialogModelView : ViewModelBase
	{
		#region Constructors

		public UserCommentDialogModelView()
		{
			OnUserCommentTextChanged();
		}

		#endregion

		#region Private Attributes

		private String charactersLeft;
		private DelegateCommand sendReportCommand;

		private string userCommentText;

		#endregion

		#region Public Properties

		public String CharactersLeft
		{
			get { return charactersLeft; }
			private set
			{
				charactersLeft = value;
				OnPropertyChanged("CharactersLeft");
			}
		}

		public ICommand SendReportCommand
		{
			get
			{
				if (null == sendReportCommand)
					sendReportCommand = new DelegateCommand(ExecuteSendReportCommand);
				return sendReportCommand;
			}
		}

		public String UserCommentText
		{
			get { return userCommentText; }
			set
			{
				userCommentText = value;
				OnUserCommentTextChanged();
			}
		}

		#endregion

		#region Private Members

		private void ExecuteSendReportCommand()
		{
			if (String.IsNullOrEmpty(UserCommentText)) return;

			WebServices.Instance.ReportUserComment(UserCommentText);
		}

		private void OnUserCommentTextChanged()
		{
			CharactersLeft = !String.IsNullOrEmpty(UserCommentText)
			                 	?
			                 		String.Format(Resources.UserCommentCharactersLeft, 500 - userCommentText.Length)
			                 	:
			                 		String.Format(Resources.UserCommentCharactersLeft, 500);
		}

		#endregion
	}
}